package com.thlws.study.rabbitmq.config;

import com.thlws.study.rabbitmq.internal.RmqConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author HanleyTang 2019-08-02
 */
@Component
public class RetryConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());

//    @Retryable(value= {Exception.class},maxAttempts = 3,backoff = @Backoff(delay = 5000L,multiplier = 1))
//    public void retry() throws Exception {
//        System.out.println("do something...");
//        throw new RemoteAccessException("RPC调用异常");
//    }

    /***
     * 消息重发完成任然失败会调用此处
     * 这里对失败消息进行转发处理
     */
    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, RmqConst.dead_letter_exchange, RmqConst.dead_letter_routing_key);
    }


//    public void recover(){
//        RetryTemplate retryTemplate = new RetryTemplate();
//        retryTemplate.registerListener(new RetryListener(){
//
//            @Override
//            public <T, E extends Throwable> boolean open(RetryContext retryContext, RetryCallback<T, E> retryCallback) {
//                return false;
//            }
//
//            @Override
//            public <T, E extends Throwable> void close(RetryContext retryContext, RetryCallback<T, E> retryCallback, Throwable throwable) {
//            }
//
//            @Override
//            public <T, E extends Throwable> void onError(RetryContext retryContext, RetryCallback<T, E> retryCallback, Throwable throwable) {
//
//            }
//        });
//
//        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));
//        retryTemplate.setBackOffPolicy(new NoBackOffPolicy());
//        RetryInterceptorBuilder.stateless().retryOperations(retryTemplate).build();
//    }



}
